﻿/******************************************************************************
* File description: BoxLicense.qml
* Function: 授权校验页面
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0

import "../components"

Item{


    id: root
    property bool hasActive: false
    property string usrname: "";
    property string limitDate: ""

    visible: licenseFalse || timeFalse

    property bool licenseFalse: false
    property bool timeFalse: false

    height: parent.height
    width: parent.width


    MouseArea{
        anchors.fill: parent
    }

    Connections{
        target: CLicense
        onShowLicensePage: {
//            console.log("触发激活显示")
            root.licenseFalse = true
        }onShowTimeExceptionPage: {
            root.timeFalse = true
        }
    }

    /** @brief 注册，将授权码发给后端判断 */
    function register(){

        if(input_license.getText() === ""){
            toast.show("请填入完整授权码", 2)
            return
        }

        var res = CLicense.registSystemLicense(input_license.getText())
        if(res){
            toast.show("激活成功", 0)
        }else{
            toast.show("激活失败", 1)
        }
        refresh()
    }

    /** @brief 刷新激活信息 */
    function refresh(){


        // 更新激活信息
        var info = CLicense.getSystemLicenseInfo()
        usrname = info["UserName"]
        limitDate = info["LimitDate"]

        hasActive = CLicense.checkSystemLicense()
    }


    onVisibleChanged: {
        input_license.setText("")
        input_uuid.setText("")
        refresh()
    }

    anchors.fill: parent

    Rectangle{      // 背景遮罩
        anchors.fill: parent
        opacity: 0.2
        color: "black"
    }

    Rectangle{      // 弹窗
        visible: root.licenseFalse && !(root.timeFalse)
        radius: 5*dp
        anchors.centerIn: parent
        anchors.verticalCenterOffset: -10*dp
        border.color: "#AAAAAA"

        width: 620*dp;  height: 490*dp
        // 阴影
        layer.enabled: true
        layer.effect: DropShadow {
            radius: 12*dp
            samples: 43
            transparentBorder: true
            color: "#4D555555"
        }


        Column{
            // 激活信息
            anchors.fill: parent
            anchors.margins: 40*dp
            spacing: 20*dp
            Item{
                height: 10*dp; width: parent.width
                Text{
                    text: "软件激活"
                    font.pixelSize: config.fontsize+2*dp
                    font.bold: true
                }
            }
            Rectangle{
                height: 2*dp
                width: parent.width
                color: "#1677FF"
            }

            Row{
                spacing: 20*dp
                CustomInputText{
                    id: input_uuid
                    editWidth: 400*dp
                }
                PushButton{
                    title: "获取机器码"
                    defaultMode2: true
                    onReleased: {
                        input_uuid.setText(CLicense.getUUID())
                    }
                }
            }
            Row{
                spacing: 20*dp
                CustomInputText{
                    id: input_license
                    title: "请输入授权码"
                    titleWidth: 100*dp
                    editWidth: 340*dp
                }
                PushButton{
                    title: "激活"
                    width: 80*dp
                    defaultMode1: true
                    onReleased: {
                        register()
                    }
                }
            }

            Rectangle{
                border.color: "#CCCCCC"
                width: parent.width
                height: 180*dp
                Column{
                    anchors.fill: parent
                    spacing: 10*dp
                    anchors.margins: 20*dp
                    Text{
                        text: "[当前激活信息]"
                        font.pixelSize: config.fontsize
                    }Text{
                        text: "用户信息: " + (usrname===""?"无":usrname)
                        font.pixelSize: config.fontsize
                    }Text{
                        text: "有效期限: " + (limitDate==""?"未激活":(hasActive?limitDate:limitDate+"(已过期)"))
                        font.pixelSize: config.fontsize
                    }
                }
            }

            Item{
                width: parent.width
                height: 40*dp
                PushButton{
                    isDisabled: ! hasActive
                    anchors.centerIn: parent
                    title: "关闭"
                    defaultMode2: true
                    onReleased: {
                        licenseFalse = false
                    }
                }
            }
        }

    }

    Rectangle{      // 时间异常弹窗

        visible: root.timeFalse
        radius: 5*dp
        anchors.centerIn: parent
        anchors.verticalCenterOffset: -10*dp
        border.color: "#AAAAAA"

        width: 420*dp;  height: 100*dp
        Text{
            text: "检测到系统时间异常\n请重启软件且保证系统时间未被修改过后再试"
            anchors.centerIn: parent
        }

        // 阴影
        layer.enabled: true
        layer.effect: DropShadow {
            radius: 12*dp
            samples: 43
            transparentBorder: true
            color: "#4D555555"
        }

    }

    Toast{
        id: toast
        anchors.horizontalCenter: parent.horizontalCenter
        y: 80*dp
    }

}
